# -*- coding:utf-8 -*-
# @Date    : 2017-08-11
# @Function: 处理用户简历数据
# @Author  : wenyq

import json
import xlsxwriter
global error_num


def resume_workbook(filename):
    f = open(filename, 'r', encoding='utf-8')
    loads = json.loads(f.read())

    workbook = xlsxwriter.Workbook('resume.xlsx')  # 创建一个excel文件
    education_background = workbook.add_worksheet('education_background')  # 创建一个"教育背景"工作表对象
    extracurricular_practice = workbook.add_worksheet('extracurricular_practice')  # 创建一个"课外实践"工作表对象
    internship_intention = workbook.add_worksheet('internship_intention')  # 创建一个"实习意向"工作表对象
    project_experience = workbook.add_worksheet('project_experience')  # 创建一个"项目经历"工作表对象
    work_experience = workbook.add_worksheet('work_experience')  # 创建一个"工作经历"工作表对象

    # eb_headings = ['uid','degree', 'major', 'schoolId', 'schoolName', 'studyContent', 'studyEndAt', 'studyStartAt']
    eb_headings = headings(loads=loads, key='education_background')
    education_background.write_row('A1', eb_headings)
    # ep_headings = ['uid','position', 'practiceCity', 'practiceCityId', 'practiceContent', 'practiceEndAt', 'practiceName', 'practiceStartAt']
    ep_headings = headings(loads=loads, key='extracurricular_practice')
    extracurricular_practice.write_row('A1', ep_headings)
    # ii_headings = ['uid','daysWeekly', 'degree', 'expectCity', 'expectCityId', 'expectDailyWage', 'expectPosition', 'graduateAt', 'internEndAt', 'internStartAt', 'liveCity', 'liveCityId', 'major', 'workDate']
    ii_headings = headings_intention(loads=loads, key='internship_intention')
    internship_intention.write_row('A1', ii_headings)
    # pe_headings = ['uid','position', 'projectCity', 'projectCityId', 'projectContent', 'projectEndAt', 'projectName', 'projectStartAt']
    pe_headings = headings(loads=loads, key='project_experience')
    project_experience.write_row('A1', pe_headings)
    # we_headings = ['uid','companyName', 'position', 'workCity', 'workCityId', 'workContent', 'workEndAt', 'workStartAt']
    we_headings = headings(loads=loads, key='work_experience')
    work_experience.write_row('A1', we_headings)

    writerow(loads=loads, sheet=education_background, key='education_background')
    writerow(loads=loads, sheet=extracurricular_practice, key='extracurricular_practice')
    writerow_intention(loads=loads, sheet=internship_intention, key='internship_intention')
    writerow(loads=loads, sheet=project_experience, key='project_experience')
    writerow(loads=loads, sheet=work_experience, key='work_experience')


def headings(loads, key):
    for i in range(len(loads['children'])):
        if len(loads['children'][i][key]) != 0:
            for j in range(len(loads['children'][i][key])):
                head = []
                head.append('uid')
                for k in loads['children'][i][key][j]:
                    head.append(k)
                print("表头", key, head)
                return head
            break


def headings_intention(loads, key):
    for i in range(len(loads['children'])):
        if loads['children'][i][key] != "" or type(loads['children'][i][key]) != str:
            head = []
            head.append('uid')
            # print(loads['children'][i][key])
            for k in loads['children'][i][key]:
                head.append(k)
            print("表头", key, head)
            return head


# 简历内容写入excel --不可用于实习意向。---数组型适用
def writerow(loads, sheet, key):
    num = 1
    for i in range(len(loads['children'])):
        for j in range(len(loads['children'][i][key])):
            if loads['children'][i][key][j] !="":
                try:
                    temp = []
                    num = num+1
                    temp.append(loads['children'][i]['uid'])
                    # print(loads['children'][i]['uid'])
                    for k in loads['children'][i][key][j]:
                        temp.append(loads['children'][i][key][j][k])
                        sheet.write_row("A%d"%num, temp)
                    # print(temp)
                except Exception as e:
                    print(e)
                    error_num += 1
                    break


# 简历内容写入excel --用于实习意向。---字典型适用
def writerow_intention(loads, sheet, key):
    num = 1
    for i in range(len(loads['children'])):
        if loads['children'][i][key] != "":
            temp = []
            num = num+1
            temp.append(loads['children'][i]['uid'])
            for k in loads['children'][i][key]:
                temp.append(loads['children'][i][key][k])
                sheet.write_row("A%d"%num, temp)


if __name__ == '__main__':
    # filename = 'Sheet1.json'
    filename = 'orion_user_resume.json'
    error_num = 0
    resume_workbook(filename)
    print("数据处理完成，数据格式错误总计：%s！!"%error_num)
